<?php
declare(strict_types=1);

namespace App\Service;
use App\Constants\StrStatus;
use App\Constants\XZXResponseCode;
use App\Exception\XZXValidationException;
use App\Model\Dao\MchInfoDao;
use App\Model\Dao\MchTradeWebsiteDao;
use App\Model\Enum\CommonReviewStatusEnum;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Cassandra\Custom;
use Hyperf\Di\Annotation\Inject;

class MchTradeWebsiteService
{
    /**
     * @Inject()
     * @var MchTradeWebsiteDao
     */
    private $MchTradeWebsiteDao;

    /**
     * @Inject()
     * @var MchInfoDao
     */
    private $MchInfoDao;

    public function get($param)
    {
        $result = $this->MchTradeWebsiteDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($result['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        if($param['source'] == 1){
            if($result['mchId'] != $param['mchId']){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }
        
        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);
        (isset($result['reviewTime']) && !empty($result['reviewTime'])) && $result['reviewTime'] = DateUtils::getDateTime($result['reviewTime']);

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function del($param)
    {
        $result = $this->MchTradeWebsiteDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($result['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        if($param['source'] == 1){
            if($result['mchId'] != $param['mchId']){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }
        
        try {
            $this->MchTradeWebsiteDao->del($param['id'],false);
        } catch (\Throwable $th) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DEL_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function add($param)
    {
        $data = $param;        
        $data['id'] = CustomUtils::createUUID();
        $data['status'] = 1;
        $data['updateBy']   = $param['createBy'];
        $data['createTime'] = time();
        unset($data['isSuperRole']);
        if ($param['source'] == 2) {
            $data['reviewStatus']   = 1;
            $data['reviewTime']     = time();
            $data['reviewBy']       = $param['createBy'];
        } else {
            $data['reviewStatus']   = 0;
        }

        // 检查
        $mch = $this->MchInfoDao->get($param['mchId']);
        if (empty($mch)) {
            // throw new XZXValidationException("商户不存在");
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_MERCHANT));
        }
        $isAdded = $this->MchTradeWebsiteDao->getByMchIdAndUrl($param['mchId'], $param['url']);
        if ($isAdded) {
            // throw new XZXValidationException("商户网站已存在");
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::MCH_TRADE_WEBSITE_IS_EXIST));
        }
        $data['mchNo'] = $mch['mchNo'];
        try {
            $this->MchTradeWebsiteDao->insert($data);
        } catch (\Throwable $th) {
             CustomUtils::xzxDebug($th->getMessage());
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::ADD_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function edit($param)
    {
        $info = $this->MchTradeWebsiteDao->get($param['id']);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($info['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        if($param['source'] == 1){
            if($info['mchId'] != $param['mchId']){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        // 检查
        $mch = $this->MchInfoDao->get($param['mchId']);
        if (empty($mch)) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_MERCHANT));
        }

        $id = $param['id'];
        $data = array();

        foreach($param as $k => $v){
            if($k == "id" || $k == "status"){
                continue;
            }            
            if($k == 'reviewStatus' && $param['source'] == 2){
                $data['reviewBy'] = $param['createBy'];
                $data['reviewTime'] = time();
            }
            if(isset($v)){
                $data[$k] = $v;
            }
        }
        unset($data['isSuperRole']);
        unset($data['mchIdList']);
        // // 平台添加的就更新状态
        // if ($param['source'] == 2) {
        //     $data['reviewStatus'] = $param['reviewStatus'];
        //     $data['status'] = $param['status'];
            
        // } else {
        //     $data['reviewStatus'] = 0;
        // }
        
        try {
            $this->MchTradeWebsiteDao->update($id, $data);
        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th);
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::EDIT_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];        

        $total = $this->MchTradeWebsiteDao->getCount($param);
        $list = $this->MchTradeWebsiteDao->getList($param, $page, $limit);

        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            (isset($v['reviewTime']) && !empty($v['reviewTime'])) && $v['reviewTime'] = DateUtils::getDateTime($v['reviewTime']);
            $v['strReviewStatus'] = CommonReviewStatusEnum::getLabel($v['reviewStatus']);
            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }    
}